/***************************
 * 新定义变量、供全局使用
 **************************/
var _ds_id = "fx_jh";
var _ds_item_id = "fx_jh_item";
var _bill_title = "进货单";


var a;
var b;
//主表字段缓存
var clons = {};
//明细字段缓存
var itemClons = {};

var tabDiv = "tabPanel";
//缓存明细对象
var peItemList = new Array();
//缓存查询出的主表对象
var peList = new Array();
//最后点击的记录数
var lastClickRow = -1;

$(function(){
	a = document.documentElement.clientHeight - 110;
 	b = document.documentElement.clientWidth - 20;
	$('#parentTabPanel').window('setTitle',_bill_title);
	
	//初始化添加到货单窗口
 	$('#infoWin').window({
	    width:document.documentElement.clientWidth,  
	    height:document.documentElement.clientHeight,  
	    title:_bill_title+'详情',
	    resizable:false,
	    closable:true,
	    maximizable:false,
	    minimizable:false
	}); 
 	
 	$('#infoWin').window('close');
 	
 	//初始化添加到货窗口
 	$('#modWin').window({
	    width:document.documentElement.clientWidth,  
	    height:document.documentElement.clientHeight,  
	    title:'修改'+_bill_title,
	    resizable:false,
	    closable:true,
	    maximizable:false,
	    minimizable:false
	}); 
 	
 	$('#modWin').window('close');
 	
	//初始化添加到货窗口
 	$('#addWin').window({  
	    width:document.documentElement.clientWidth,  
	    height:document.documentElement.clientHeight,  
	    title:'添加'+_bill_title,
	    resizable:false,
	    closable:true,
	    maximizable:false,
	    minimizable:false
	}); 
 	$('#addWin').window('close');
	
 	//初始化到货详情窗口
 	
	//加载按钮
	loadButns();
	//初始化datagrid
	loadDategrid();
	//加载字段
	loadClns();
	//加载对照信息
	loadDzSource();
	//初始化详情界面
	initInfoWin();
	
});

//初始化详情界面
function initInfoWin(){
	//加载详情的对照信息
	if($('#infoWinTable').html()==''){
		//加载主表字段
		$.ajax({
			type:'post',
			url:'../../framework/columnList',
			data:{'colId':_ds_id},
			success:function(data){
				if(data ==""){
					alert("数据为空");
				}else{
					//请求一个组织列的方法
					divName='infoWinTable';//条件字段
					makeColumns(data,divName);
					makeColumns(data,'modWinTable');//填充修改页面
					//加载对照信息
					var formdiv = "infoWinForm";
					var dzIdstr = serializeEDIType(formdiv);
	                $.ajax({
	                    url:'../../dz/queryDzList',
	                    type:'post',
	                    data:{dzIds:dzIdstr},
	                    success:function(data){
	                            //下拉框
	                            fillSelect(formdiv,data);
	                            fillSelect('modWinForm',data);//填充修改页面
	                    }
	                });
				}
			}
		});
		
		//加载Datagrid按钮
		initItemInfoDg();
		
		//加载详情主表按钮
		$.ajax({
			type:'post',
			url:'../../framework/buttonsListC',
			data:{'butnId':"fxJhInfo"},
			success:function(data){
				if(data == ""){
					alert("按钮数据为空");
				}else{
					var buttons = data;
					var butHTML = "";
					for(var i = 0; i < buttons.length; i++){
						butHTML += formatButtons(buttons[i]);
					}
					$('#infoWinBut').html(butHTML);
					$.parser.parse('#infoWinBut');  //重新编译界面
				}
			}
		});
		
		//加载详情明细按钮
		$.ajax({
			type:'post',
			url:'../../framework/buttonsListC',
			data:{'butnId':"fxJhItemInfo"},
			success:function(data){
				if(data == ""){
					alert("按钮数据为空");
				}else{
					var buttons = data;
					var butHTML = "";
					for(var i = 0; i < buttons.length; i++){
						butHTML += formatButtons(buttons[i]);
					}
					$('#infoItemBut').html(butHTML);
					$.parser.parse('#infoItemBut');  //重新编译界面
				}
			}
		});
		//加载修改页面主表按钮
		$.ajax({
			type:'post',
			url:'../../framework/buttonsListC',
			data:{'butnId':"fxJhMod"},
			success:function(data){
				if(data == ""){
					alert("按钮数据为空");
				}else{
					var buttons = data;
					var butHTML = "";
					for(var i = 0; i < buttons.length; i++){
						butHTML += formatButtons(buttons[i]);
					}
					$('#modWinBut').html(butHTML);
					$.parser.parse('#modWinBut');  //重新编译界面
				}
			}
		});
		
		//加载修改页面明细按钮
		$.ajax({
			type:'post',
			url:'../../framework/buttonsListC',
			data:{'butnId':"fxJhItemMod;fxJhMod"},
			success:function(data){
				if(data == ""){
					alert("按钮数据为空");
				}else{
					var buttons = data;
					var butHTML = "";
					for(var i = 0; i < buttons.length; i++){
						butHTML += formatButtons(buttons[i]);
					}
					$('#modItemBut').html(butHTML);
					$.parser.parse('#modItemBut');  //重新编译界面
				}
			}
		});
	}
}
//初始化详情明细字段
function initItemInfoDg(){
	$.ajax({
		type:'post',
		url:'../../framework/listQueryJson',
		data:{'dsId':_ds_item_id,'colId':_ds_item_id,'execFlag':''},
		success:function(data){
			if(data==""){
				alert("数据为空");
			}else{
				//给datagrid赋列值，没有数据
				minfodg(data,'infoItemDatagrid');
				mItemDatagrid(data,'modItemDatagrid');
			}
		}
	});
}

//初始化Datagrid
function minfodg(data,dataGId){ //传入的是queryJson 里面就有columns和rows
	var lastIndex;
	$('#'+dataGId).datagrid({
		iconCls:'icon-save',
		height:a,
		width:b,
		striped: true,				 
		collapsible:true,
		autoRowHeight: false,		
		sortOrder: 'desc',
		remoteSort: false,
		pageList:[30,50,100,150],
		columns:[[]],
		pagination:true,
		rownumbers:true,
		singleSelect:true
	});
	var dataGridEl = $('#'+dataGId);
	dataGridEl.datagrid(jointColumn(data.columns));//根据配置选项，生成datagrid  
}

//加载按钮
function loadButns(){
	$.ajax({
		type:'post',
		url:'../../framework/buttonsListC',
		data:{'butnId':"fxJhM"},
		success:function(data){
			if(data == ""){
				alert("按钮数据为空");
			}else{
				var buttons = data;
				var butHTML = "";
				for(var i = 0; i < buttons.length; i++){
					butHTML += formatButtons(buttons[i]);
				}
				$('#BUTTONS').html(butHTML);
				$('#QUERYBUT').html(butHTML);
				$.parser.parse('#BUTTONS');  //重新编译界面
				$.parser.parse('#QUERYBUT');  //重新编译界面
			}
		}
	});
}


//加载数据表格
function loadDategrid(){
	$.ajax({
		type:'post',
		url:'../../framework/listQueryJson',
		data:{'dsId':_ds_id,'colId':_ds_id,'execFlag':''},
		success:function(data){
			if(data==""){
				alert("数据为空");
			}else{
				//给datagrid赋列值，没有数据
				mdatagrid(data);
			}
		}
	});
}

//初始化Datagrid
function mdatagrid(data){ //传入的是queryJson 里面就有columns和rows
	var lastIndex;
	$('#DATALIST').datagrid({
		iconCls:'icon-save',
		height:a,
		width:b,
		striped: true,				 
		collapsible:true,
		autoRowHeight: false,		
		sortOrder: 'desc',
		remoteSort: false,
		pageList:[30,50,100,150],
		columns:[[]],
		pagination:true,
		rownumbers:true,
		singleSelect:true,
		onDblClickRow:function(rowIndex,rowData){
			//显示详情	
			showInfo(rowData);
		}
	});
	var dataGridEl = $('#DATALIST');
	dataGridEl.datagrid(jointColumn(data.columns));//根据配置选项，生成datagrid  
}

//加载选题字段
function loadClns(){
	$.ajax({
			type:'post',
			url:'../../framework/columnList',
			data:{'colId':_ds_id},
			success:function(data){
				if(data ==""){
					alert("数据为空");
				}else{
					//请求一个组织列的方法
					clons = data;
					divName='QUERYCLN';//条件字段
					makeQueryColumns(clons,divName);
				}
			}
	});
}

/************************************/
//加载节点的对照信息
function loadDzSource(){
	$.ajax({
		url:'../../dz/queryDzList',
		type:'post',
		data:{dzIds:'bs_station;dz_bumen;dz_zhuguan'},
		success:function(data){
			//xtType 选题类型     author 作者  station 站点 oparator 用户
			station = data[0];
			bumen = data[1];
			zhuguan = data[2];
		}
	});
}

//翻译方法

function stationFormatter(value){
	for(var i=0; i<station.length; i++){
		if (station[i].ID == value) return station[i].NAME;
	}
	return value;
}

function bumenFormatter(value){
	for(var i=0; i<bumen.length; i++){
		if (bumen[i].ID == value) return bumen[i].NAME;
	}
	return value;
}

function zhuguanFormatter(value){
	for(var i=0; i<zhuguan.length; i++){
		if (zhuguan[i].ID == value) return zhuguan[i].NAME;
	}
	return value;
}


function chenjieFormatter(value){
	return '承接人未设置';
}


function jiesuanFormatter(value){
	return '结算货源未设置';
}

/************************************/

//初始化Datagrid
function mItemDatagrid(data,dataGDiv){ //传入的是queryJson 里面就有columns和rows
	var lastIndex;
	$('#'+dataGDiv).datagrid({
		iconCls:'icon-save',
		height:a,
		width:b,
		striped: true,				 
		collapsible:true,
		autoRowHeight: false,		
		sortOrder: 'desc',
		remoteSort: false,
		pageList:[30,50,100,150],
		columns:[[]],
		pagination:true,
		rownumbers:true,
		singleSelect:true,
		onDblClickRow:function(rowIndex,rowData){
			if (lastIndex != rowIndex){
				$('#'+dataGDiv).datagrid('endEdit', lastIndex);
				$('#'+dataGDiv).datagrid('beginEdit', rowIndex);
			}
			lastIndex = rowIndex;
		}
	});
	var dataGridEl = $('#'+dataGDiv);
	dataGridEl.datagrid(jointColumn(data.columns));//根据配置选项，生成datagrid  
}

//初始化添加商品字段信息
function lPDatagrid(){
	$.ajax({
		type:'post',
		url:'../../framework/listQueryJson',
		data:{'dsId':'bs_product','colId':'bs_product','execFlag':''},
		success:function(data){
			if(data==""){
				alert("数据为空");
			}else{
				//给datagrid赋列值，没有数据
				mPDatagrid(data);
			}
		}
	});
}

//初始化添加商品字段信息Datagrid
function mPDatagrid(data){ //传入的是queryJson 里面就有columns和rows
	var lastIndex;
	$('#productDatagrid').datagrid({
		iconCls:'icon-save',
		height:a,
		width:b,
		striped: true,				 
		collapsible:true,
		autoRowHeight: false,		
		sortOrder: 'desc',
		remoteSort: false,
		pageList:[30,50,100,150],
		frozenColumns:[[
		    {field:'ck',checkbox:true}
		]],
		columns:[[]],
		pagination:true,
		rownumbers:true,
		onDblClickRow:function(rowIndex,rowData){
		}
	});
	var dataGridEl = $('#productDatagrid');
	dataGridEl.datagrid(jointColumn(data.columns));//根据配置选项，生成datagrid  
}

